<?php
require_once APP . '/application/admincontrollers/AdminAction.php';
/**
 * 后台管理主页
 * @package admin
 */
class SpiderController extends AdminAction
{
    
	public function init()
    {
    }


    /**
     * 采集：http://www.123sf.com/
     * 实际地址为：http://119.147.145.223:8011/
     */
    public function do123sfAction()
    {
    	set_time_limit(5 * 60);
    	$t = microtime(true);
    	$source = 1;
    	$this->setAutoRender(false);
    	
    	echo '开始采集' . '<br />' ;
    	
    	$content = file_get_contents("http://119.147.145.224:8012");
    	$content = iconv("GBK", "UTF-8", $content);
    	
    	/**$content = '
    	<dl class="nowopen" style="background:#FFFF00" onmouseover=this.style.backgroundColor="#FFFFFF" onmouseout=this.style.backgroundColor="#FFFF00">
<dd class="mc"><a href=go.htm?u=http://www2.haofu521.com target=_blank>１·８０复古新区</a></dd>
<dd class="ip"><a href=go.htm?u=http://www2.haofu521.com target=_blank>５３级奖励开天</a></dd>
<dd class="sj">12月5日/〖通宵推荐〗</dd>
<dd class="xl"><font color=green>千兆双线</font></dd>
<dd class="js">１·８０无英雄＼小极品＼不变态＼稳定<font color=#0000FF>-推荐</font></dd>
<dd class="qq">无GM长期</dd>
<dt class="xx"><a href=go.htm?u=http://www2.haofu521.com target=_blank>点击查看</a></dt>
</dl>
ddd';/**/
    	
    	$pattern = '<dl__R2__>_S_<dd__R2__><a__R2__>__R__</a></dd>_S_<dd__R2__><a__R2__>__R__</a></dd>_S_<dd__R2__>__R__</dd>_S_<dd__R2__>__R__</dd>_S_<dd__R2__>__R__</dd>_S_<dd__R2__>__R__</dd>_S_<dt__R2__><a href=go.htm?u=__R__ target=_blank>点击查看</a></dt>_S_</dl>';
    	$pattern = preg_quote($pattern, "/");
    	$pattern = str_replace("__R__", '(.*?)',$pattern);
    	$pattern = str_replace("__R2__", '.*?',$pattern);
    	$pattern = str_replace("_S_", '\s*',$pattern);
    	$pattern = '/' . $pattern . '/ms';
    	
    	preg_match_all($pattern, $content, $matchs, PREG_SET_ORDER);
    	// 数据解析
    	$arr_data = array();
    	$pubtime = time();
    	$pubip = $this->getIp(true);
    	foreach ($matchs as $v){
    		$data_row = array();
    		$data_row['source'] = $source;
    		$data_row['pubtime'] = $pubtime;
    		$data_row['pubip'] = $pubip;
    		$data_row['isdayfine'] = 0;
    		$data_row['isyellow'] = 0;
    		$data_row['ishot'] = 0;
    		
    		$data_row['name'] = trim(strip_tags($v[1]));
    		$data_row['ip'] = trim(strip_tags($v[2]));
    		
    		$data_row['sdatetime'] = strip_tags($v[3]);
    		if (strpos($data_row['sdatetime'], '精品全天') !== false){
    			$data_row['sdatetime'] = date("Y-m-d 12:30:00");
    			$data_row['isdayfine'] = 1;
    		}elseif (strpos($data_row['sdatetime'], '〖通宵推荐〗') !== false){
    			$data_row['sdatetime'] = date("Y-m-d 2:30:00", strtotime("+1 day"));
    			$data_row['ishot'] = 1;
    		}
    		
    		$data_row['routeline'] = strip_tags($v[4]);
    		$data_row['intro'] = strip_tags($v[5]);
    		if (stripos($data_row['intro'], '-推荐') !== false){
    			$data_row['isyellow'] = 1;
    			$data_row['intro'] = str_replace("-推荐", "", $data_row['intro']);
    		}
    		
    		$data_row['qq'] = strip_tags($v[6]);
    		$data_row['url'] = strip_tags($v[7]);
    		
    		// 处理时间格式 12月5日/下午17点开放 12月5日/下午16点30分开放 12月5日/中午12点开放 12月5日/上午9点30分开放 12月5日/上午9点开放 12月5日/晚上18点开放 12月5日/晚上18点30分开放
    		
    		// 去除处理过时间的
    		$y = date("Y");
    		if(!($data_row['isdayfine'] == 1 || $data_row['ishot'] == 1)){
    			if (preg_match("/(\d+)月(\d+)日\/[^\d]+(\d+)[^\d]+(\d+|\d*)/", $data_row['sdatetime'], $tmatches)){
    				if (empty($tmatches[4]))
						$tmatches[4] = '00';
    				$tstr = $y . '-' . $tmatches[1] . '-' . $tmatches[2] . ' ' . $tmatches[3] . ':' . $tmatches[4] . ':00';
    				$data_row['sdatetime'] = date("Y-m-d H:i:00", strtotime($tstr));
    			}else{
    				$log = date("Y-m-d H:i:s") . ' - 时间错误:' . $data_row['sdatetime'] . "\r\n" . $v[0] . "\r\n\r\n\r\n";
    				file_put_contents(APP . '/cache/' . $source . '.log', $log, FILE_APPEND);
    				continue;
    			}
    		}
    		
    		if ($data_row['isyellow'] || $data_row['isdayfine'])
    			$arr_data[] = $data_row;
    	}
    	
    	// 处理重复数据 根据最近1000条记录来查询 如果名称和ip组合都一样，则为重复
    	$arr_db_data = $this->db->fetchAll("select name,ip from sfdata where source={$source} order by id desc limit 1000");
    	$arr_exists = array();
    	foreach ($arr_db_data as $v){
    		$arr_exists[$v['name'].$v['ip']] = true;
    	}
    	foreach ($arr_data as $index => $row){    		
    		if (isset($arr_exists[$row['name'] . $row['ip']])){
    			unset($arr_data[$index]);
    		}else{
    			$arr_exists[$row['name'] . $row['ip']] = true;
    		}
    	}
    	
    	// 插入数据
    	$this->batchInsert($arr_data);
    	
    	echo '采集成功，共加入' . count($arr_data) . '条数据<br />';
    	echo '共用时：' . (microtime(true) - $t) . 's';
    }
    
    
    
  	/**
     * 采集：www.yaosf.com
     * 实际地址为：http://www.yaosf.com/list.html
     */
    public function doyaosfAction()
    {
    	set_time_limit(5 * 60);
    	$t = microtime(true);
    	$source = 2;
    	$this->setAutoRender(false);
    	
    	echo '开始采集' . '<br />' ;
    	
	    $content = file_get_contents("http://www.yaosf.com/list.html");
	    $content = iconv("GBK", "UTF-8", $content);

    	/**$content = '
    	<tr onmouseover="hover(this)"><td><a target="_blank" href="go.htm?http://www.amopr.com/">╊╊古墓龙魂╱╊</a></td><td><a target="_blank" href="go.htm?http://www.amopr.com/">╳元宝比例超高╳</a></td><td>12月/7日/上午11点</td><td class="blue">独家三线不卡</td><td>装备好爆人好</td><td>╊靓装╊皇冠╊送会员╊PK很激情╊打装备很</td><td><a class="red" target="_blank" href="go.htm?http://www.amopr.com/">详细介绍</a></td></tr>
ddd';/**/
    	
    	$pattern = '<tr__R2__><td><a__R2__>__R__</a></td><td__R2__><a__R2__>__R__</a></td><td__R2__>__R__</td><td__R2__>__R__</td><td__R2__>__R__</td><td__R2__>__R__</td><td><a__R2__go.htm?__R__">__R2__</a></td></tr>';
    	
    	$pattern = preg_quote($pattern, "/");
    	$pattern = str_replace("__R__", '(.*?)',$pattern);
    	$pattern = str_replace("__R2__", '.*?',$pattern);
    	//$pattern = str_replace("_S_", '\s*',$pattern);
    	$pattern = '/' . $pattern . '/ms';
    	
    	preg_match_all($pattern, $content, $matchs, PREG_SET_ORDER);
    	
    	// 数据解析
    	$arr_data = array();
    	$pubtime = time();
    	$pubip = $this->getIp(true);
    	foreach ($matchs as $v){
    		$data_row = array();
    		$data_row['source'] = $source;
    		$data_row['pubtime'] = $pubtime;
    		$data_row['pubip'] = $pubip;
    		$data_row['isdayfine'] = 0;
    		$data_row['isyellow'] = 0;
    		$data_row['ishot'] = 0;
    		$data_row['isfinehot'] = 0;
    		
    		$data_row['name'] = trim(strip_tags($v[1]));
    		$data_row['ip'] = trim(strip_tags($v[2]));
    		
    		$data_row['sdatetime'] = strip_tags($v[3]);
    		if (strpos($data_row['sdatetime'], '精品总固顶') !== false){
    			$data_row['sdatetime'] = date("Y-m-d 12:30:00");
    			$data_row['isdayfine'] = 1;
    		}elseif (strpos($data_row['sdatetime'], '通宵固顶') !== false){
    			$data_row['sdatetime'] = date("Y-m-d 2:30:00", strtotime("+1 day"));
    			$data_row['isfinehot'] = 1;
    		}elseif (strpos($data_row['sdatetime'], '通宵推荐') !== false){
    			$data_row['sdatetime'] = date("Y-m-d 2:30:00", strtotime("+1 day"));
    			$data_row['ishot'] = 1;
    		}else{
    			$data_row['isyellow'] = 1;
    		}
    		
    		$data_row['routeline'] = strip_tags($v[4]);
    		$data_row['intro'] = strip_tags($v[6]);
    		
    		$data_row['qq'] = strip_tags($v[5]);
    		$data_row['url'] = trim(strip_tags($v[7]), '\\');
    		
    		// 处理时间格式 12月/6日/上午11点 12月5日/下午17点开放 12月5日/下午16点30分开放 12月5日/中午12点开放 12月5日/上午9点30分开放 12月5日/上午9点开放 12月5日/晚上18点开放 12月5日/晚上18点30分开放
    		
    		// 去除处理过时间的
    		$y = date("Y");
    		if(!($data_row['isdayfine'] == 1 || $data_row['ishot'] == 1 || $data_row['isfinehot'] == 1)){
    			if (preg_match("/(\d+)[^\d]+(\d+)日\/[^\d]+(\d+)[^\d]+(\d+|\d*)/", $data_row['sdatetime'], $tmatches)){
    				if (empty($tmatches[4]))
						$tmatches[4] = '00';
    				$tstr = $y . '-' . $tmatches[1] . '-' . $tmatches[2] . ' ' . $tmatches[3] . ':' . $tmatches[4] . ':00';
    				$data_row['sdatetime'] = date("Y-m-d H:i:00", strtotime($tstr));
    			}else{
    				$log = date("Y-m-d H:i:s") . ' - 时间错误:' . $data_row['sdatetime'] . "\r\n" . $v[0] . "\r\n";
    				$log.= print_r($data_row, true) . "r\n\r\nr\n\r\n";
    				file_put_contents(APP . '/cache/' . $source . '.log', $log, FILE_APPEND);
    				continue;
    			}
    		}
    		
    		if ($data_row['isyellow'] || $data_row['isdayfine'] || $data_row['isdayfine']|| $data_row['ishot']|| $data_row['isfinehot'])
    			$arr_data[] = $data_row;
    	}
    	
    	// 处理重复数据 根据最近1000条记录来查询 如果名称和ip组合都一样，则为重复
    	$arr_db_data = $this->db->fetchAll("select name,ip from sfdata where source={$source} order by id desc limit 1000");
    	$arr_exists = array();
    	foreach ($arr_db_data as $v){
    		$arr_exists[$v['name'].$v['ip']] = true;
    	}
    	foreach ($arr_data as $index => $row){    		
    		if (isset($arr_exists[$row['name'] . $row['ip']])){
    			unset($arr_data[$index]);
    		}else{
    			$arr_exists[$row['name'] . $row['ip']] = true;
    		}
    	}
    	
    	// 插入数据
    	$this->batchInsert($arr_data);
    	
    	echo '采集成功，共加入' . count($arr_data) . '条数据<br />';
    	echo '共用时：' . (microtime(true) - $t) . 's';
    }
    
    
    /**
     * 采集：www.haocq.com
     * 实际地址为：http://www.haocq.com/
     */
    public function dohaocqAction()
    {
    	set_time_limit(5 * 60);
    	$t = microtime(true);
    	$source = 3;
    	$this->setAutoRender(false);
    	
    	echo '开始采集' . '<br />' ;
    	
	    $content = file_get_contents("http://www.haocq.com/");
	    $content = iconv("GBK", "UTF-8", $content);
	    

    	/**$content = <<<EOT
<tr bgcolor=\"#ffff98\" onmouseover=javascript:this.bgColor=\'#FFFFFF\' onmouseout=javascript:this.bgColor=\'#ffff98\'><TD width=11%> <a href=http://www.dxtf.net target="_blank"><font color=#000000>１.７６战无不胜</font></a></TD><TD width=11%><a href=http://www.dxtf.net target="_blank">最新毁灭火爆</a></TD><TD class=font_R width=16%>12月/8日/14点/30分开放</TD><TD align=center width=10%>双线不卡</TD><TD>影刺毁灭公司运行盛大新装非RMB玩家也-<font color=#ff0000>推荐</font></TD><TD align=center width=10%>上线送4级火</TD><TD align=center width=7%><a href=http://www.dxtf.net target="_blank">点击查看</a></TD></tr>
    	 <TR bgColor=#ffff98 onmouseover=javascript:this.bgColor='#FFFFFF' onmouseout=javascript:this.bgColor='#ffff98'>
<TD width=11%> <a href=http://dlq.sc8866.com/1.html target='_blank'><font color=#000000>超超超超变变变变</font></a></TD>
<TD width=11%><a href=http://dlq.sc8866.com/1.html target='_blank'>上线９９９９９级</a></TD>
<TD class=font_R width=16%><b><span class=STYLE8><font color=#ff0000>91ww.COM</font><font color=Purple>-精品全天推荐</font></span></b></TD>
<TD align=center width=10%>▓人满为患▓</TD>
<TD>狂暴速度刀〓突破６５５３５级〓终极狂 -<font color=#ff0000>推荐</font></TD>
<TD align=center width=10%>体验⑧⑧⑧万</TD>
<TD align=center width=7%><a href=http://dlq.sc8866.com/1.html target='_blank'>点击查看</a></TD>
</TR>
EOT;
/**/
    	// __R2__ __R__ _S_
    	$pattern = '<tr__R2__>_S_
    	<TD__R2__>_S_<a__R2__>__R__</a>_S_</TD>_S_
    	<TD__R2__><a__R2__>__R__</a>_S_</TD>_S_
    	<TD__R2__>__R__</TD>_S_
    	<TD__R2__>__R__</TD>_S_
    	<TD__R2__>__R__</TD>_S_
    	<TD__R2__>__R__</TD>_S_
    	<TD__R2__>_S_<a_S_href=__R___S_target__R2__>__R2__</a>_S_</TD>_S_
    	</tr>';
    	
    	$pattern = str_replace(array("\r", "\n", ' ', "\t"), "", $pattern);
    	$pattern = preg_quote($pattern, "/");
    	$pattern = str_replace("__R__", '(.*?)',$pattern);
    	$pattern = str_replace("__R2__", '.*?',$pattern);
    	$pattern = str_replace("_S_", '\s*',$pattern);
    	$pattern = '/' . $pattern . '/mis';
    	
    	preg_match_all($pattern, $content, $matchs, PREG_SET_ORDER);
    	
    	// 数据解析
    	$arr_data = array();
    	$pubtime = time();
    	$pubip = $this->getIp(true);
    	foreach ($matchs as $v){
    		$data_row = array();
    		$data_row['source'] = $source;
    		$data_row['pubtime'] = $pubtime;
    		$data_row['pubip'] = $pubip;
    		$data_row['isdayfine'] = 0;
    		$data_row['isyellow'] = 0;
    		$data_row['ishot'] = 0;
    		$data_row['isfinehot'] = 0;
    		
    		$data_row['name'] = trim(strip_tags($v[1]));
    		$data_row['ip'] = trim(strip_tags($v[2]));
    		
    		$data_row['sdatetime'] = strip_tags($v[3]);
    		if (strpos($data_row['sdatetime'], '精品全天推荐') !== false){
    			$data_row['sdatetime'] = date("Y-m-d 12:30:00");
    			$data_row['isdayfine'] = 1;
    		}elseif (strpos($data_row['sdatetime'], '通宵固顶') !== false){
    			$data_row['sdatetime'] = date("Y-m-d 2:30:00", strtotime("+1 day"));
    			$data_row['isfinehot'] = 1;
    		}elseif (strpos($data_row['sdatetime'], '通宵推荐') !== false){
    			$data_row['sdatetime'] = date("Y-m-d 2:30:00", strtotime("+1 day"));
    			$data_row['ishot'] = 1;
    		}else{
    			$data_row['isyellow'] = 1;
    		}
    		
    		$data_row['routeline'] = strip_tags($v[4]);
    		$data_row['intro'] = strip_tags($v[5]);
    		
    		$data_row['qq'] = strip_tags($v[6]);
    		$data_row['url'] = trim(strip_tags($v[7]), '\\');
    		
    		// 处理时间格式 12月/6日/上午11点 12月5日/下午17点开放 12月5日/下午16点30分开放 12月5日/中午12点开放 12月5日/上午9点30分开放 12月5日/上午9点开放 12月5日/晚上18点开放 12月5日/晚上18点30分开放  12月/8日/14点/30分开放
    		
    		// 去除处理过时间的
    		$y = date("Y");
    		if(!($data_row['isdayfine'] == 1 || $data_row['ishot'] == 1 || $data_row['isfinehot'] == 1)){
    			if (preg_match("/(\d+)[^\d]+(\d+)[^\d]+(\d+)[^\d]+(\d+|\d*)/", $data_row['sdatetime'], $tmatches)){
    				if (empty($tmatches[4]))
						$tmatches[4] = '00';
    				$tstr = $y . '-' . $tmatches[1] . '-' . $tmatches[2] . ' ' . $tmatches[3] . ':' . $tmatches[4] . ':00';
    				$data_row['sdatetime'] = date("Y-m-d H:i:00", strtotime($tstr));
    			}else{
    				$log = date("Y-m-d H:i:s") . ' - 时间错误:' . $data_row['sdatetime'] . "\r\n" . $v[0] . "\r\n";
    				$log.= print_r($data_row, true) . "r\n\r\nr\n\r\n";
    				file_put_contents(APP . '/cache/' . $source . '.log', $log, FILE_APPEND);
    				continue;
    			}
    		}
    		
    		if ($data_row['isyellow'] || $data_row['isdayfine'] || $data_row['isdayfine']|| $data_row['ishot']|| $data_row['isfinehot'])
    			$arr_data[] = $data_row;
    	}
    	
    	// 处理重复数据 根据最近1000条记录来查询 如果名称和ip组合都一样，则为重复
    	$arr_db_data = $this->db->fetchAll("select name,ip from sfdata where source={$source} order by id desc limit 1000");
    	$arr_exists = array();
    	foreach ($arr_db_data as $v){
    		$arr_exists[$v['name'].$v['ip']] = true;
    	}
    	foreach ($arr_data as $index => $row){    		
    		if (isset($arr_exists[$row['name'] . $row['ip']])){
    			unset($arr_data[$index]);
    		}else{
    			$arr_exists[$row['name'] . $row['ip']] = true;
    		}
    	}
    	
    	// 插入数据
    	$this->batchInsert($arr_data);
    	
    	echo '采集成功，共加入' . count($arr_data) . '条数据<br />';
    	echo '共用时：' . (microtime(true) - $t) . 's';
    }
    
    
    
    /**
     * 采集：www.80sf.com
     * 实际地址为：http://www.80sf.com/game.html
     */
    public function do80sfAction()
    {
    	set_time_limit(5 * 60);
    	$t = microtime(true);
    	$source = 4;
    	$this->setAutoRender(false);
    	
    	echo '开始采集' . '<br />' ;
    	

	    //$content = file_get_contents("http://www.80sf.com/game.html");
	    $content = file_get_contents("http://www.laouc.com/gg.html");
	    $content = iconv("GBK", "UTF-8", $content);

    	/**$content = <<<EOT
<dl class="nowopen" style="background:#FFFFFF" onmouseover=this.style.backgroundColor="#FFFF00" onmouseout=this.style.backgroundColor="#FFFFFF"><dd class="mc"><a href=go.htm?u=http://wt.kxmir2.com/ target=_blank><font color=#FF0000>１.８０飞龙元素</font></a></dd><dd class="ip"><a href=go.htm?u=http://wt.kxmir2.com/ target=_blank>致命一击元素飛龍</a></dd><dd class="sj"><font color=#FF0000>1月6日/上午10点开放</font></dd><dd class="xl"><font color=blue>经典网通</font></dd><dd class="js">☆☆══道士招飛龍═仙踪林版══☆☆</dd><dd class="qq">绝对长久开放</dd><dd class="xx"><a href=go.htm?u=http://wt.kxmir2.com/ target=_blank>点击查看</a></dd><dt class="xj"><font color=#FF0000>正在开放</font></dt></dl>
EOT;
/**/

    	// __R2__ __R__ _S_
    	$pattern = '<dl__R2__>_S_
    	<dd__R2__><a__R2__>__R__</a>_S_</dd>_S_
    	<dd__R2__>__R__</dd>_S_
    	<dd__R2__>__R__</dd>_S_
    	<dd__R2__>__R__</dd>_S_
    	<dd__R2__>__R__</dd>_S_
    	<dd__R2__>__R__</dd>_S_
    	<dd__R2__><a_S_href=go.htm?u=__R___S_target__R2__>点击查看</a></dd>_S_
    	<dt__R2__>__R__</dt></dl>';
    	
    	$pattern = str_replace(array("\r", "\n", ' ', "\t"), "", $pattern);
    	$pattern = preg_quote($pattern, "/");
    	$pattern = str_replace("__R__", '(.*?)',$pattern);
    	$pattern = str_replace("__R2__", '.*?',$pattern);
    	$pattern = str_replace("_S_", '\s*',$pattern);
    	$pattern = '/' . $pattern . '/mis';
    	
    	preg_match_all($pattern, $content, $matchs, PREG_SET_ORDER);

    	// 数据解析
    	$arr_data = array();
    	$pubtime = time();
    	$pubip = $this->getIp(true);
    	foreach ($matchs as $v){
    		$data_row = array();
    		$data_row['source'] = $source;
    		$data_row['pubtime'] = $pubtime;
    		$data_row['pubip'] = $pubip;
    		$data_row['isdayfine'] = 0;
    		$data_row['isyellow'] = 0;
    		$data_row['ishot'] = 0;
    		$data_row['isfinehot'] = 0;
    		$data_row['isbest'] = 0;
    		
    		$data_row['name'] = trim(strip_tags($v[1]));
    		$data_row['ip'] = trim(strip_tags($v[2]));
    		
    		$data_row['sdatetime'] = strip_tags($v[3]);
    		if (strpos($data_row['sdatetime'], '精品固顶') !== false){
    			$data_row['sdatetime'] = date("Y-m-d 12:30:00");
    			$data_row['isbest'] = 1;
    		}elseif (strpos($data_row['sdatetime'], '全天套黄') !== false){
    			$data_row['sdatetime'] = date("Y-m-d 12:30:00");
    			$data_row['isdayfine'] = 1;
    		}elseif (strpos($data_row['sdatetime'], '固顶通宵') !== false){
    			$data_row['sdatetime'] = date("Y-m-d 2:30:00", strtotime("+1 day"));
    			$data_row['isfinehot'] = 1;
    		}elseif (strpos($data_row['sdatetime'], '通宵推荐') !== false){
    			$data_row['sdatetime'] = date("Y-m-d 2:30:00", strtotime("+1 day"));
    			$data_row['ishot'] = 1;
    		}else{
    			$data_row['isyellow'] = 1;
    		}
    		
    		$data_row['routeline'] = strip_tags($v[4]);
    		$data_row['intro'] = strip_tags($v[5]);
    		
    		$data_row['qq'] = strip_tags($v[6]);
    		$data_row['url'] = trim(strip_tags($v[7]), '\\');
    		
    		// 处理时间格式 1月6日/上午10点30分开放
    		
    		// 去除处理过时间的
    		$y = date("Y");
    		if(!($data_row['isdayfine'] == 1 || $data_row['ishot'] == 1 || $data_row['isfinehot'] == 1 || $data_row['isbest'])){
    			if (preg_match("/(\d+)[^\d]+(\d+)[^\d]+(\d+)[^\d]+(\d+|\d*)/", $data_row['sdatetime'], $tmatches)){
    				if (empty($tmatches[4]))
						$tmatches[4] = '00';
    				$tstr = $y . '-' . $tmatches[1] . '-' . $tmatches[2] . ' ' . $tmatches[3] . ':' . $tmatches[4] . ':00';
    				$data_row['sdatetime'] = date("Y-m-d H:i:00", strtotime($tstr));
    			}else{
    				$log = date("Y-m-d H:i:s") . ' - 时间错误:' . $data_row['sdatetime'] . "\r\n" . $v[0] . "\r\n";
    				$log.= print_r($data_row, true) . "r\n\r\nr\n\r\n";
    				file_put_contents(APP . '/cache/' . $source . '.log', $log, FILE_APPEND);
    				continue;
    			}
    		}
    		
    		if ($data_row['isyellow'] || $data_row['isbest'] || $data_row['isdayfine'] || $data_row['isdayfine']|| $data_row['ishot']|| $data_row['isfinehot'])
    			$arr_data[] = $data_row;
    	}
    	
    	echo count($arr_data);
    	
    	// 处理重复数据 根据最近1000条记录来查询 如果名称和ip组合都一样，则为重复
    	$arr_db_data = $this->db->fetchAll("select name,ip from sfdata where source={$source} order by id desc limit 1000");
    	$arr_exists = array();
    	foreach ($arr_db_data as $v){
    		$arr_exists[$v['name'].$v['ip']] = true;
    	}
    	foreach ($arr_data as $index => $row){    		
    		if (isset($arr_exists[$row['name'] . $row['ip']])){
    			unset($arr_data[$index]);
    		}else{
    			$arr_exists[$row['name'] . $row['ip']] = true;
    		}
    	}
    	
    	// 插入数据
    	$this->batchInsert($arr_data);
    	
    	echo '采集成功，共加入' . count($arr_data) . '条数据<br />';
    	echo '共用时：' . (microtime(true) - $t) . 's';
    }
    
    
    
    /**
     * 批量插入
     *
     * @param unknown_type $data
     */
    private function batchInsert($data)
    {
    	if (empty($data))
    		return;
    	
    	$keys = array_keys(current($data));
    	
    	// 构造语句进行插入
    	$sql_pre = "insert into `sfdata`( " . implode(",", $keys) .") values";
    	
    	$sql_val = '';
    	$comma = "";
    	$i = 0;
    	foreach ($data as $row){
    		$str_val = '';
    		$str_comma = '';
    		foreach ($keys as $key){
    			$vv = mysql_real_escape_string($row[$key]);
    			$str_val .= $str_comma . '"' . $vv . '"';
    			$str_comma = ',';
    		}
    		$sql_val .= $comma . "({$str_val})";
    		$comma = ',';
    		$i++;
    		
    		if ($i % 100 == 0){// 每100个为一个批次
    			$this->db->query($sql_pre . $sql_val);
    			$sql_val = '';
    			$comma = '';
    		}
    	}
    	
    	if (!empty($sql_val)){
    		$this->db->query($sql_pre . $sql_val);
    	}
    }
}